package TUILayer;//makes this class a part of the TUILayer package
import java.util.Scanner;
import ControlLayer.*;//The TUILayer needs to know the ControlLayer
import java.util.regex.Pattern;

public class AddressBookUI
{   

    private AddressCtr addrCtr;//reference to the ControlLayer
    

    public AddressBookUI(ControlCollector collector)
    {
        addrCtr = collector.getA();
       
    }
   
    /**
     * This method begins the creation of TUI
     */
    public void start()
    {
       
        addressMenu();
    }
    
    /**
     * This method creates a menu for the address TUI, it also collects input from the user and calls one of 5 functions connected with AddressBook.
     */
    public void addressMenu()
    {
       boolean exit = false;
       while(!exit)//that is: while exit is false
       {
          int choice = writeAddressBookMenu(); 
          if(choice == 1)
          {   //show person
              getPerson();
          }
          else{
              if(choice == 2){
                    addPerson();
              }  
              else{
                  if(choice == 3){
                      deletePerson();
                      //delete person
                  }  
                  else{
                      if(choice == 4){
                          // update person
                          updatePerson();
                      }
                      else{
                          if(choice == 5){
                               listAllPersons();
                            }
                          else{
                              exit = true;
                           }//end else
                        }//end else
                    }//end else
                }//end else
            }//end else
            
       }//end while
    }
    
    /**
     * This method provides a prints a menu for method addressMenu.
     */
    public int writeAddressBookMenu()
    {      // makes an object keyboard to read input from the screen
            Scanner keyboard = new Scanner(System.in);
            System.out.println("\f *** AddressBook Menu ***");
            System.out.println(" (1) Show information about person");
            System.out.println(" (2) Add person");
            System.out.println(" (3) Delete person");
            System.out.println(" (4) Update person");
            System.out.println(" (5) List all persons");
            System.out.println(" (6) Return");
            System.out.print("\n Make your choice: ");
            
            int choice = keyboard.nextInt();
            return choice;
    }
        
     /**
     * This method collects input, which is an id of the person to be added to AddressBook, from the user.
     * Since the id has to be a number, this function will loop untill user provides it with correct input.
     */
    public long inputPersonsID()
    {   
        boolean ok = false;
        long id = 0;
        while(!ok)
        {
        // makes an object keyboard to have input from the console
           Scanner keyboard = new Scanner(System.in);
           System.out.println(" Write Id:  ");
           try{
             id = keyboard.nextLong();
             ok = true;
           }
           catch (Exception e)
            {
             System.out.println("You must enter a number -  try again");
             String input = keyboard.nextLine();
           }
        }//end while
        return id;
    }
    
    /**
     * This method collects input, which is a name of the person to be added to AddressBook, from the user.
     */
    public String inputName()
    {
         // makes an object keyboard to have input from the screen
         Scanner keyboard = new Scanner(System.in);  
         System.out.println(" Write Name:  ");
         String name = keyboard.nextLine();
         return name;
    }
   
    /**
     * This method collects input, which is a postal code of the person to be added to AddressBook, from the user.
     */
    public String inputPostalCode()
    {
         // makes an object keyboard to have input from the screen
         Scanner keyboard = new Scanner(System.in);  
         System.out.println(" Write Postal Code:  ");
         String code = keyboard.nextLine();
         return code;
    }
    
    /**
     * This method collects input, which is a city in which lives the person that is to be added to AddressBook, from the user.
     */
    public String inputCity()
    {
         // makes an object keyboard to have input from the screen
         Scanner keyboard = new Scanner(System.in);  
         System.out.println(" Write City:  ");
         String city = keyboard.nextLine();
         return city;
    } 
    
    /**
     * This method collects input, which is a phone number of the person to be added to AddressBook, from the user.
     * This method uses regular expression to check whether the input is correct for number.
     * A correct phone number may optionally start from '+' and 2 digits directional. The number should also be no shorter than 8 digits and no longer than 15 (with the 2 digit directional).
     * Groups of digits can be separeted with dash ('-') or may be provided in a single row. If the input does not fulfill these requirements the user is prompted to enter it again.
     */
    public String inputPhone()
    {
         // makes an object keyboard to have input from the screen
         Scanner keyboard = new Scanner(System.in);  
         
         boolean wrong = true;
         String phone = "";
         String newPhone ="";
         while (wrong)
         {
             System.out.println(" Write Phone Number:  ");
             phone = keyboard.nextLine();
             if(phone.matches("\\+*[0-9]+(:?-[0-9]+)*"))
             {
                 if (phone.contains("-"))
                 {
                     newPhone = phone.replaceAll("-","");
                 }
                 else
                 {
                     newPhone = phone;
                 }
                 
                 if((newPhone.length() >= 8 && newPhone.length() <= 13) || (newPhone.length() >=11 && newPhone.length() <= 16 && newPhone.startsWith("+")))
                 {
                     wrong = false;
                 }
                 else
                 {
                     System.out.println("The phone was typed incorrectly, type it again:");
                 }
             }
             else
             {
                 System.out.println("The phone was typed incorrectly, type it again:");
             }
         }
         return phone;
    } 
     
    /**
     * This method collects input, which is an address of the person that is to be added to AddressBook, from the user.
     */
    public String inputAddress()
    {
        // makes an object keyboard to read input from the console
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" Write Address: ");
        String address = keyboard.nextLine();
        return address;
    }
    
    /**
     * This method prints information about a desired person.
     */
    private void getPerson()
    {
        long id = inputPersonsID();
        if(addrCtr.isIt(id))
        {
            addrCtr.printPerson(id);
        }
        else
        {
            System.out.println("No such person in address book");
        }
        waitForInput();
    }   
    
    /**
     * This method collects data from the user and adds a new person to the address book.
     */
    private void addPerson()
    {
        long id = inputPersonsID();
        if(addrCtr.isIt(id))
        {
            System.out.println("The ID is already used by some other person");
        }
        else
        {
            String name = inputName();
            String address = inputAddress();
            String postalCode = inputPostalCode();
            String city = inputCity();
            String phone = inputPhone();
            addrCtr.createPerson(id, name, address, postalCode, city, phone);
        }
        waitForInput();
    }
    
    /**
     * This method takes input from the user to update data of the person with desired ID.
     */
    private void updatePerson()
    {
        
        long id = inputPersonsID();
        if (addrCtr.isIt(id))
        {
            String name = inputName();
            String address = inputAddress();
            String postalCode = inputPostalCode();
            String city = inputCity();
            String phone = inputPhone();
            addrCtr.updatePerson(id, name, address, postalCode, city, phone);
        }
        else
        {
            System.out.println("No such person in address book");
        }
        waitForInput();
    }
    
    /**
     * This method removes a person from an address book.
     */
    private void deletePerson()
    {
        long id = inputPersonsID();
        addrCtr.deletePerson(id);
        waitForInput();
    }
    
    /**
     * This method prints info of all persons in the address book
     */
    private void listAllPersons()
    {
        System.out.println("\nPeople:");
        addrCtr.listAllPersons();
        waitForInput();
    }
    
    
    /**
     * This method makes the TUI wait for ENTER so it is possibble to read all the data on the screen.
     */
    private void waitForInput()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("\nPress ENTER to continue\n");
        keyboard.nextLine();
    }
}
            
        
